import jieba
import jieba.analyse
import os
from flask import request,Blueprint,jsonify
from pojo.Content import Content
hotwords = Blueprint('hotwords', __name__)
targetTxt="hotwords.txt"
#分词热词
def hotwordCreate(data):

    if  not os.path.isfile(targetTxt):
        fd = open(targetTxt, mode="w", encoding="utf-8")
        fd.close()
    targetFile=open(targetTxt, 'w')
    for el in data:
        if el.content is not "":
            seg = jieba.cut(el.content, cut_all = False)
            output = ' '.join(seg)
            targetFile.write(output)
            targetFile.write('n')
    targetFile.close()
    print("分词结束且保存进入文件中")
@hotwords.route("/getHotWords")
def hotwordRead():
    file=open(targetTxt, 'r')
    text = file.readlines()
    keywords = jieba.analyse.extract_tags(str(text), topK=10, withWeight=True, allowPOS=())
    list=[]
    for hotWord in keywords:
        list.append({
            "hotWord":hotWord[0]
        })
    return jsonify(list)
@hotwords.route("/echartsHotWordDisplay")
def echartsHotWordDisplay():
    file = open(targetTxt, 'r')
    text = file.readlines()
    keywords = jieba.analyse.extract_tags(str(text), topK=15, withWeight=True, allowPOS=())
    list = []
    dataAxis=[]
    data=[]
    for hotWord in keywords:
        list.append({
            "hotWord": hotWord[0]
        })
        dataAxis.append(hotWord[0])
        data.append(hotWord[1])
    return jsonify({
        "dataAxis":dataAxis,
        "data":data
    })
# list=[]
# c1=Content()
# c1.content="我是最棒的啊"
# c2=Content()
# c2.content="张三李四王五"
# c3=Content()
# c3.content="王五赵六天齐"
# c4=Content()
# c4.content="测试得方案"
# list.append(c1)
# list.append(c2)
# list.append(c3)
# list.append(c4)
# hotwordCreate(list)
# hotwordRead()